package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.DeviceNode;
import com.android.equipment.dao.DeviceNode_;

import io.objectbox.Box;
import io.objectbox.query.QueryBuilder;

public class DaoDeviceNode {
    static Box<DeviceNode> deviceNodeBox;
    private static DaoDeviceNode instance;

    private DaoDeviceNode() {
    }

    public static DaoDeviceNode getInstance() {
        if (instance == null) {
            instance = new DaoDeviceNode();
            deviceNodeBox = ObjectBox.get().boxFor(DeviceNode.class);
        }
        return instance;
    }

    public void addDeviceNode(DeviceNode deviceNode) {
        deviceNodeBox.put(deviceNode);
    }

    public void updateDeviceNode(DeviceNode deviceNode) {
        deviceNodeBox.put(deviceNode);
    }

    public void deleteDeviceNode(DeviceNode deviceNode){
        deviceNodeBox.remove(deviceNode);
    }


    public DeviceNode getDeviceNode(String deviceCode, String node){
        return deviceNodeBox.query()
                .equal(DeviceNode_.deviceCode,deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .equal(DeviceNode_.node,node, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .build().findUnique();
    }

    public void deleteAllDeviceNode(){
        deviceNodeBox.removeAll();
    }
}
